package handler

import (
	"godco/internal/utils"
	"net/http"
	"strconv"

	"github.com/zeromicro/go-zero/rest/httpx"
	"godco/internal/logic"
	"godco/internal/svc"
	"godco/internal/types"
)

func UserSignHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.SignRequest
		if err := httpx.Parse(r, &req); err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}

		l := logic.NewUserSignLogic(r.Context(), svcCtx)
		resp, err := l.UserSign(&req)
		if err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
		} else {
			httpx.OkJsonCtx(r.Context(), w, resp)
		}
	}
}

func CaptureEmailCodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.EmailCodRequest
		if err := httpx.Parse(r, &req); err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}

		l := logic.NewUserSignLogic(r.Context(), svcCtx)
		resp, err := l.CaptureEmailCode(&req)
		if err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
		} else {
			httpx.OkJsonCtx(r.Context(), w, resp)
		}
	}
}

func UserSignedPageHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.UserSignedPageRequest
		if err := httpx.Parse(r, &req); err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}

		l := logic.NewUserSignLogic(r.Context(), svcCtx)
		resp, err := l.UserSignedPage(&req)
		if err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
		} else {
			httpx.OkJsonCtx(r.Context(), w, resp)
		}
	}
}

func SignedUserPageHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.SignedUserPageRequest
		if err := httpx.Parse(r, &req); err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}

		l := logic.NewUserSignLogic(r.Context(), svcCtx)
		resp, err := l.SignedUserPage(&req)
		if err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
		} else {
			httpx.OkJsonCtx(r.Context(), w, resp)
		}
	}
}

func UserCancelSignHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.UserCancelSignRequest
		if err := httpx.Parse(r, &req); err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}
		l := logic.NewUserSignLogic(r.Context(), svcCtx)
		err := l.UserCancelSign(&req)
		if err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
		} else {
			httpx.OkJsonCtx(r.Context(), w, map[string]interface{}{
				"code": 200,
				"msg":  "success",
			})
		}
	}
}

func SingedUserDetailHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.SignedUserDetailRequest
		if err := httpx.Parse(r, &req); err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}
		l := logic.NewUserSignLogic(r.Context(), svcCtx)
		resp, err := l.SignedUserDetail(&req)
		if err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
		}
		httpx.OkJsonCtx(r.Context(), w, map[string]interface{}{
			"content": resp,
			"code":    200,
			"msg":     "success",
		})
	}
}

func SingedListDetailHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.SignedUserListRequest
		if err := httpx.Parse(r, &req); err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}
		l := logic.NewUserSignLogic(r.Context(), svcCtx)
		resp, err := l.SignedListDetail(&req)
		if err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
		}
		httpx.OkJsonCtx(r.Context(), w, map[string]interface{}{
			"content": resp,
			"code":    200,
			"msg":     "success",
		})
	}
}

func UserSignedDetailHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.UserSignedDetailRequest
		if err := httpx.Parse(r, &req); err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}
		l := logic.NewUserSignLogic(r.Context(), svcCtx)
		resp, err := l.UserSignedDetail(&req)
		if err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
		} else {
			_, err := utils.GetInstallationAppInfo(strconv.FormatInt(req.InstallationId, 10), svcCtx)
			if err != nil {
				httpx.ErrorCtx(r.Context(), w, err)
				return
			}
			httpx.OkJsonCtx(r.Context(), w, resp)
		}
	}
}
